<?php
/**
 * CreateTime: 2023/2/1 11:27
 */

namespace App\Tools\USails;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;

class USailsBasic
{
    private $client;

    public function __construct()
    {
        $this->client = USailsClient::getInstance();
    }

    private function formatRes($res,$canList = false){
        if($res->code!=2000){
            return ResponseFunc::returnJson(
                'error',
                null,
                $res->msg,
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,
            );
        }
        if($canList){
            return ResponseFunc::returnJson(
                'ok',
                [
                    'current_page' => $res->data->page->page,
                    'last_page' => $res->data->page->lastPage,
                    'per_page' => $res->data->page->pageSize,
                    'total' => $res->data->page->total,
                    'to' => $res->data->page->total,
                    'data' => $res->data->list,
                ],
                $res->msg,
                ResponseStatusConsts::OK_200_RESPONSE_STATUS,   
            );
        }
        return ResponseFunc::returnJson(
            'ok',
            $res->data,
            $res->msg,
            ResponseStatusConsts::OK_200_RESPONSE_STATUS,
        );
    }


    public function getExtensionsList($page = 1,$pageSize = 10){
        $path = "extensions";
        $data = [
          'page' => $page,
          'pageSize' => $pageSize
        ];
        $this->client->changeRequestType("GET");
        $res = $this->client->execute($path,$data);
        return $this->formatRes(json_decode($res),true);
    }

    public function registerCallBack(){
        $path = "organ/callback";
        $data = [
            'cdr' => "http://be.cyhzsoft.com:49005/index.php/api/meCallBack",
        ];
        $this->client->changeRequestType("POST");
        $res = $this->client->execute($path,$data);
        dd($res);
    }

    public function sendCall($callee,$caller){
        $path = "call/back";
        $data = [
            'callee' => $callee,
            'caller' => $caller,
        ];
        $this->client->changeRequestType("POST");
        $res = $this->client->execute($path,$data);
        dd($res);
    }

    private function hangupCauseEnum($case){
        switch ($case){
            case 'NO_USER_RESPONSE':
                return "未接通";
            default:
                return "正常挂断";
        }
    }

    public function getMeCdrsDetailsByUUid($uuid){
        $path = "cdr/$uuid/details";
        $this->client->changeRequestType("GET");
        $res = $this->client->execute($path,[]);
        $res = json_decode($res);
        $res->data->hangupCause = $this->hangupCauseEnum($res->data->hangupCause);
        return $this->formatRes($res);
    }

    public function getMeCdrsDetailsByUUidNotFmt($uuid){
        $path = "cdr/$uuid/details";
        $this->client->changeRequestType("GET");
        $res = $this->client->execute($path,[]);
        $res = json_decode($res);
        $res->data->hangupCause = $this->hangupCauseEnum($res->data->hangupCause);
        return $res;
    }


    public function getMeCdrsList($data){
        $path = "cdrs";
        $this->client->changeRequestType("GET");
        $res = $this->client->execute($path,$data);
        return $this->formatRes(json_decode($res),true);
    }
}